package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.StudentChangeClass;
import cn.wolfcode.crm.mapper.OfficialStudentMapper;
import cn.wolfcode.crm.mapper.StudentChangeClassMapper;
import cn.wolfcode.crm.query.StudentChangeClassQueryObj;
import cn.wolfcode.crm.service.IStudentChangeClassService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentChangeClassServiceImpl implements IStudentChangeClassService {

    @Autowired
    private StudentChangeClassMapper studentChangeClassMapper;
    @Autowired
    private OfficialStudentMapper officialStudentMapper;

    public void delete(Long id) {
        studentChangeClassMapper.deleteByPrimaryKey(id);
    }

    public void save(StudentChangeClass entity) {
        if(entity != null){
            studentChangeClassMapper.insert(entity);
        }
    }

    public void update(StudentChangeClass entity) {

        studentChangeClassMapper.updateByPrimaryKey(entity);
    }

    public StudentChangeClass get(Long id) {
        return studentChangeClassMapper.selectByPrimaryKey(id);
    }

    public List<StudentChangeClass> list() {
        return studentChangeClassMapper.selectAll();
    }

    public PageResult query(StudentChangeClassQueryObj qo) {

        //查总数
        int total = studentChangeClassMapper.queryForCount(qo);
        if(total == 0){
            return new PageResult();
        }
        //查数据
        List<StudentChangeClass> rows = studentChangeClassMapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    public void audit(Long id) {
        //判断是否审核
        StudentChangeClass studentChangeClass = studentChangeClassMapper.selectByPrimaryKey(id);
        if(studentChangeClass.getAuditState() == 1){
            throw new RuntimeException("已审核  不允许操作!");
        }
        studentChangeClassMapper.audit(id);
        //修改正式学员状态
        officialStudentMapper.updateStudyState(studentChangeClass.getBeforeStuId());
        //修改正式学员现在班级
        Long beforeStuId = studentChangeClass.getBeforeStuId();
        Long nowClassId = studentChangeClass.getNowClass().getId();
        officialStudentMapper.updateNowClass(beforeStuId,nowClassId);
    }

}
